//
//  WBMainViewController.swift
//  weibo
//
//  Created by apple on 2017/5/17.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        ///Users/apple/Desktop/swiftproject/weibo/weibo/Classses/View(试图和控制器)/Main/WBMainViewController.swift:16:10: 'setupChildControllers' is inaccessible due to 'private' protection level
       setupChildControllers()
        setupComposeButtton()
        ///Users/apple/Desktop/swiftproject/weibo/weibo/Classses/View(试图和控制器)/Main/WBMainViewController.swift:20:9: Attribute 'private' can only be used in a non-local scope
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    ///Users/apple/Desktop/swiftproject/weibo/weibo/Classses/View(试图和控制器)/Main/WBMainViewController.swift:33:1: Declaration is only valid at file scope
    // MARK: - 私有控件
     lazy var composeButton: UIButton = UIButton.init(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")

}
extension WBMainViewController {
    func composeStatus(){
        print("撰写微博")
    }

    //设置撰写按钮
     func setupComposeButtton(){
        tabBar.addSubview(composeButton)
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        //将向内缩紧的宽度减少，能够让按钮的宽度变大，盖住容错点
        let w = tabBar.bounds.width / count - 1
        
        //CGRectInset 正向内缩进，负数向外扩展
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        //按钮监听方法
        ///Users/apple/Desktop/swiftproject/weibo/weibo/Classses/View(试图和控制器)/Main/WBMainViewController.swift:56:57: Use of unresolved identifier 'composeStatus'
        composeButton.addTarget(self, action: #selector(composeStatus), for: UIControlEvents.touchUpInside)
        
    }
    //设置所有子控制器
     func setupChildControllers() {
        let array =
              [["clsName":"WBHomeViewController","title":"首页","imageName":"home"],["clsName":"WBDiscoverViewController","title":"消息","imageName":"message_center"],
               ["clsName":"UIViewController"],
               ["clsName":"WBMessageViewController","title":"发现","imageName":"discover"],["clsName":"WBProfileViewController","title":"我","imageName":"profile"]]
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(contoller(dict: dict))
        }
        self.viewControllers = arrayM
    }
    
     func contoller(dict:[String: String]) -> UIViewController {
        let bundleName = Bundle.main.namespace
        print(bundleName)
        
        //1.取得字典内容
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imgeName = dict["imageName"],
            
        let cls = NSClassFromString(Bundle.main.namespace() + "." + clsName) as? UIViewController.Type
             else {
                return UIViewController()
        }
        //1 将clsname 专、换成cls
        
        
        //2.创建试图控制器
        let vc = cls.init()
        vc.title = title
        //3.设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imgeName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imgeName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
        //4.设置tabbar的标题字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: UIControlState.normal)
        //实例化导航控制器的时候，会调用push方法将rootvc压栈
        let nav = WBNavigationController(rootViewController: vc)
        return nav
    }
}

